export default function getSearchData(platform, data) {
  let regExps = /bitrate:(\d+),format:(\w+),size:([\w.]+)/;
  let newData = [];
  let total = 0;
  switch (platform) {
    case "kg":
      total = data.data.total;
      data.data.info.forEach(item => {
        let types = [];
        if (item.filesize !== 0) {
          types.push({
            type: "128k",
            hash: item.hash,
            size: getSize(item.filesize),
            tagType: "success"
          });
        }
        if (item["320filesize"] !== 0) {
          types.push({
            type: "320k",
            hash: item["320hash"],
            size: getSize(item["320filesize"]),
            tagType: "warning"
          });
        }
        if (item.sqfilesize !== 0) {
          types.push({
            type: "flac",
            hash: item.sqhash,
            size: getSize(item.sqfilesize),
            tagType: "danger"
          });
        }
        newData.push({
          songName: item.songname,
          singerName: item.singername,
          albumName: item.album_name,
          fileName: item.filename,
          types: types
        });
      });
      break;
    case "kw":
      total = Number.parseInt(data.TOTAL);
      data.abslist.forEach(item => {
        let types = [];
        let hash = item.MUSICRID.replace("MUSIC_", "");
        let typesInfoArr = item.MINFO.split(";");
        typesInfoArr.forEach(info => {
          info = info.match(regExps);
          if (info) {
            switch (info[2]) {
              case "flac":
                types.push({
                  type: "flac",
                  size: info[3],
                  hash: hash,
                  tagType: "danger"
                });
                break;
              case "mp3":
                switch (info[1]) {
                  case "320":
                    types.push({
                      type: "320k",
                      size: info[3],
                      hash: hash,
                      tagType: "warning"
                    });
                    break;
                  case "128":
                    types.push({
                      type: "128k",
                      size: info[3],
                      hash: hash,
                      tagType: "success"
                    });
                    break;
                }
                break;
            }
          }
        });
        types.reverse();
        newData.push({
          songName: item.SONGNAME,
          singerName: item.ARTIST,
          albumName: item.ALBUM,
          fileName: null,
          types: types
        });
      });
      break;
    case "tx":
      total = data.data.song.totalnum;
      data.data.song.list.forEach(item => {
        let types = [];
        let hash = item.strMediaMid;
        if (item.size128 !== 0) {
          let size = getSize(item.size128);
          types.push({
            type: "128k",
            size: size,
            hash: hash,
            tagType: "success"
          });
        }
        if (item.size320 !== 0) {
          let size = getSize(item.size320);
          types.push({
            type: "320k",
            size: size,
            hash: hash,
            tagType: "warning"
          });
        }
        if (item.sizeflac !== 0) {
          let size = getSize(item.sizeflac);
          types.push({
            type: "flac",
            size: size,
            hash: hash,
            tagType: "danger"
          });
        }
        newData.push({
          songName: item.songname,
          singerName: item.singer.map(item=>item.name).join(","),
          albumName: item.albumname,
          fileName: null,
          types: types
        });
      });
      break;
    case "mg":
      total = Number.parseInt(data.songResultData.totalCount);
      data.songResultData.resultList.forEach(item => {
        item = item[0];
        let types = [];
        let hash = item.copyrightId;
        item.rateFormats &&
          item.rateFormats.forEach(type => {
            switch (type.formatType) {
              case "PQ":
                types.push({
                  type: "128k",
                  size: getSize(type.size),
                  hash: hash,
                  tagType: "success"
                });
                break;
              case "HQ":
                types.push({
                  type: "320k",
                  size: getSize(type.size),
                  hash: hash,
                  tagType: "warning"
                });
                break;
              case "SQ":
                types.push({
                  type: "flac",
                  size: getSize(type.size),
                  hash: hash,
                  tagType: "danger"
                });
                break;
            }
          });
        newData.push({
          songName: item.name,
          singerName: item.singers.length > 0 ? item.singers[0].name : "",
          albumName: item.albums.length > 0 ? item.albums[0].name : "",
          fileName: null,
          types: types
        });
      });
      break;
    default:
      break;
  }
  return { data: newData, total: total };
}

function getSize(limit) {
  let size = "";
  if (limit < 0.1 * 1024) {
    //小于0.1KB，则转化成B
    size = limit.toFixed(2) + "B";
  } else if (limit < 0.1 * 1024 * 1024) {
    //小于0.1MB，则转化成KB
    size = (limit / 1024).toFixed(2) + "KB";
  } else if (limit < 0.1 * 1024 * 1024 * 1024) {
    //小于0.1GB，则转化成MB
    size = (limit / (1024 * 1024)).toFixed(2) + "MB";
  } else {
    //其他转化成GB
    size = (limit / (1024 * 1024 * 1024)).toFixed(2) + "GB";
  }

  let sizeStr = size + ""; //转成字符串
  let index = sizeStr.indexOf("."); //获取小数点处的索引
  let dou = sizeStr.substr(index + 1, 2); //获取小数点后两位的值
  if (dou == "00") {
    //判断后两位是否为00，如果是则删除00
    return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2);
  }
  return size;
}
